﻿using System;
using System.Linq;
using Microsoft.SharePoint;

namespace SPDeployer.Core
{
    internal class ListManager
    {
        public SPList Create(SPWeb web, string title, string description, SPListTemplate template)
        {
            var listId = web.Lists.Add(title, description, template);
            return web.Lists[listId];
        }

        public SPList Get(SPWeb web, string title)
        {
            return web.Lists.Cast<SPList>().SingleOrDefault(l => title == l.Title);
        }

        public SPList Get(SPWeb web, Uri relativeUrl)
        {
            if(relativeUrl.IsAbsoluteUri)
                throw new UriFormatException(relativeUrl.ToString());

            return web.GetList(relativeUrl.ToString());
        }
    }
}
